﻿using Foundation.Core;
using Microsoft.Extensions.DependencyInjection;

namespace Dyao.Book.Application
{
    public abstract class TaskRuleExcutorBase : ITaskRuleExcutor
    {
        private IJsonSerializer _jsonSerializer;
        protected IJsonSerializer JsonSerializer => ServiceProvider.GetRequiredServiceReference(ref _jsonSerializer);

        public IServiceProvider ServiceProvider { get; set; }

        protected TService LazyGetRequiredService<TService>(ref TService service)
        {
            if (service != null) return service;
            service = ServiceProvider.GetService<TService>();
            return service;
        }

        public abstract string Key { get; }
        public abstract Task ExcuteAsync(TaskCenterRuleExcutingContext context);

        protected virtual T Deserialize<T>(string json)
        {
            if (json == null) return default;

            return JsonSerializer.Deserialize<T>(json);
        }
    }

}
